<template>
  <div>
    <!-- 导航 -->
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin-bottom: 30px"
    >
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row style="margin-bottom: 30px">
        <!-- 添加角色 -->
        <el-button type="primary" slot="" @click="addDialogVisible = true"
          >添加角色</el-button
        >
      </el-row>
      <el-table border :data="rolesList" style="width:100%">
        <!-- 下拉 start -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              class="bdBottom dfCenter"
              v-for="(item1, i1) in scope.row.children"
              :key="i1"
            >
              <!-- 一级 -->
              <el-col :span="6">
                <el-tag
                  class="mb40 "
                  @close="deleteRoles(scope.row, item1.id)"
                  closable
                  >{{ item1.authName }}</el-tag
                >
              </el-col>
              <el-col :span="18">
                <el-row
                  :class="['dfCenter', i2 === 0 ? '' : 'bdTop']"
                  v-for="(item2, i2) in item1.children"
                  :key="i2"
                >
                  <!-- 二级 -->
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      class="mb40"
                      closable
                      @close="deleteRoles(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                  </el-col>
                  <el-col :span="18">
                    <el-row v-for="(item3, i3) in item2.children" :key="i3">
                      <!-- 三级 -->
                      <el-col :span="24">
                        <el-tag
                          closable
                          type="warning"
                          class="mb40"
                          @close="deleteRoles(scope.row, item3.id)"
                          >{{ item3.authName }}</el-tag
                        >
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 下拉 end -->
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作">
          <!-- 编辑   按钮-->
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <!-- 删除   按钮-->
            <el-button type="danger" icon="el-icon-delete">删除</el-button>
            <!-- 分配角色  按钮 -->
            <!-- enterable  鼠标是否可以移入tooltip中 -->
            <el-tooltip
              effect="dark"
              content="分配角色"
              placement="top-start"
              :enterable="false"
            >
              <el-button
                type="warning"
                icon="el-icon-setting"
                @click="showSendRolesDialog(scope.row)"
                >分配权限</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限  对话框 -->
    <el-dialog :visible.sync="sendRolesDialogVisible" @close="sendRolesDialogClose" width="30%">
      <el-tree
        ref="reeeRef"
        :data="rightsTreeList"
        show-checkbox
        node-key="id"
        :default-expand-all= "true"
        :default-expanded-keys="keysList"
        :default-checked-keys="keysList"
        :props="defaultProps"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="sendRolesDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allowSave">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [], // 角色列表数据
      keysList: [], // 默认展开的数组列表
      sendRolesDialogVisible: false, // 控制显示隐藏分配权限对话框
      rightsTreeList: [], // 权限列表
      defaultProps: { // 分配权限对话框的渲染
        children: 'children',
        label: 'authName'
      },
      roleId: ''
    }
  },
  methods: {
    // 获取角色列表数据
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      console.log(res)
      this.rolesList = res.data
    },
    // 删除角色
    async deleteRoles(role, right) {
      // 下面的返回值是Promise   添加 await
      const result = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      console.log(result)
      if (result === 'confirm') {
        console.log(right)
        const { data: res } = await this.$http.delete(
          `roles/${role.id}/rights/${right}`
        )
        if (res.meta.status !== 200) {
          this.$message.error('删除失败！')
        }
        console.log(res)
        // this.getRolesList() // 删除之后直接调用此方法默认会关闭下拉列表  重新打开
        role.children = res.data // 重新赋值此行的children     切记不加this
      }
    },
    // 展示分配权限对话框   获取权限列表数据
    async showSendRolesDialog (role) {
      // 当前分配权限的id
      this.roleId = role.id
      this.sendRolesDialogVisible = true
      const { data: res } = await this.$http.get('rights/tree')
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error('获取权限列表失败！')
      }
      this.rightsTreeList = res.data
      this.getLeafKeys(role, this.keysList)
    },
    // 关闭分配权限对话框时
    sendRolesDialogClose () {
      this.keysList = []
    },
    // 通过递归的形式或者角色下所有三级权限的id，并保存到defKeys中
    getLeafKeys(node, arr) {
      // 没有children  是三级节点
      if (!node.children) {
        // 把要展开的id放到新的数组
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
      console.log(arr)
    },
    // 确定分配权限
    async allowSave() {
      const allowList = [
        ...this.$refs.reeeRef.getCheckedKeys()
      ]
      const strList = allowList.join(',')
      console.log(strList)
      console.log(this.roleId)
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: strList })
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error('分配权限失败！')
      }
      this.$message.success('分配权限成功！')
      this.sendRolesDialogVisible = false
      this.getRolesList()
    }
  },
  created() {
    this.getRolesList()
  }
}
</script>
<style>
.mb40 {
  margin: 30px;
}
.bdBottom {
  border-bottom: 1px solid #eee;
}
.bdTop {
  border-top: 1px solid #eee;
}
.dfCenter {
  display: flex;
  align-items: center;
}

</style>
